<template>
  <div class="settings-page">
    <div class="settings-header">
      <h2>{{ $t('settings.title') }}</h2>
    </div>

    <div class="settings-content">
      <el-card class="settings-card">
        <div slot="header" class="card-header">
          <i class="el-icon-s-tools"></i>
          <span>{{ $t('settings.language') }}</span>
        </div>
        
        <div class="setting-item">
          <div class="setting-label">
            <h3>{{ $t('settings.language') }}</h3>
            <p>{{ $t('settings.languageDescription') }}</p>
          </div>
          
          <div class="setting-control">
            <el-radio-group v-model="currentLanguage" @change="changeLanguage" size="medium">
              <el-radio-button label="en">
                <span class="language-option">
                  <img src="" alt="EN" class="flag-icon" />
                  {{ $t('settings.english') }}
                </span>
              </el-radio-button>
              <el-radio-button label="zh">
                <span class="language-option">
                  <img src="" alt="ZH" class="flag-icon" />
                  {{ $t('settings.chinese') }}
                </span>
              </el-radio-button>
            </el-radio-group>
          </div>
        </div>
      </el-card>

      <el-card class="settings-card">
        <div slot="header" class="card-header">
          <i class="el-icon-info"></i>
          <span>{{ $t('common.info') }}</span>
        </div>
        
        <div class="info-content">
          <p>{{ $t('settings.languageDescription') }}</p>
          <p>{{ currentLanguage === 'en' ? 'Current language: English' : '当前语言：中文' }}</p>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex'
import i18nMixin from '@/mixins/i18nMixin'

export default {
  name: 'Settings',
  mixins: [i18nMixin],
  
  data() {
    return {
      currentLanguage: this.$i18n.locale
    }
  },
  
  computed: {
    ...mapState(['settings'])
  },
  
  mounted() {
    // Load current settings
    this.loadSettings()
    if (this.settings && this.settings.language) {
      this.currentLanguage = this.settings.language
    }
  },
  
  methods: {
    ...mapActions(['loadSettings', 'saveSettings']),
    
    async changeLanguage(lang) {
      // Update i18n locale
      this.$i18n.locale = lang
      
      // Update Element UI locale
      const ElementLocale = await import('element-ui/lib/locale')
      if (lang === 'zh') {
        const zhLocale = await import('element-ui/lib/locale/lang/zh-CN')
        ElementLocale.use(zhLocale.default)
      } else {
        const enLocale = await import('element-ui/lib/locale/lang/en')
        ElementLocale.use(enLocale.default)
      }
      
      // Save to store and localStorage
      await this.saveSettings({
        ...this.settings,
        language: lang
      })
      
      this.$message.success(this.$t('settings.saveSuccess'))
    }
  }
}
</script>

<style scoped>
.settings-page {
  height: 100%;
  overflow-y: auto;
  background: linear-gradient(135deg, #f5f7fa 0%, #e9ecef 100%);
  padding: 30px;
}

.settings-header {
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 2px solid #e2e8f0;
}

.settings-header h2 {
  margin: 0;
  font-size: 28px;
  font-weight: 700;
  color: #1e293b;
  letter-spacing: -0.5px;
}

.settings-content {
  max-width: 800px;
  margin: 0 auto;
}

.settings-card {
  margin-bottom: 24px;
  border-radius: 12px;
  border: none;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  transition: all 0.3s;
}

.settings-card:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.card-header {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 18px;
  font-weight: 600;
  color: #1e293b;
}

.card-header i {
  color: #667eea;
  font-size: 20px;
}

.setting-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 0;
  border-bottom: 1px solid #f1f5f9;
}

.setting-item:last-child {
  border-bottom: none;
  padding-bottom: 0;
}

.setting-label h3 {
  margin: 0 0 8px;
  font-size: 16px;
  font-weight: 600;
  color: #1e293b;
}

.setting-label p {
  margin: 0;
  font-size: 14px;
  color: #64748b;
}

.setting-control {
  flex-shrink: 0;
}

.language-option {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 8px;
  min-width: 100px;
}

.flag-icon {
  width: 24px;
  height: 16px;
  border-radius: 2px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.el-radio-group {
  display: flex;
  gap: 12px;
}

.el-radio-button {
  border-radius: 8px !important;
  overflow: hidden;
}

.el-radio-button__inner {
  padding: 8px 16px !important;
  border-radius: 8px !important;
  border-color: #e2e8f0 !important;
  font-weight: 500;
  transition: all 0.3s;
}

.el-radio-button__inner:hover {
  background: #f8fafc;
  border-color: #667eea !important;
  color: #667eea;
}

.el-radio-button.is-active .el-radio-button__inner {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border-color: #667eea !important;
  color: white;
  box-shadow: 0 4px 8px rgba(102, 126, 234, 0.3);
}

.info-content {
  padding: 16px 0;
}

.info-content p {
  margin: 0 0 12px;
  font-size: 14px;
  color: #475569;
  line-height: 1.6;
}

.info-content p:last-child {
  margin-bottom: 0;
  font-weight: 500;
  color: #1e293b;
}

@media (max-width: 768px) {
  .settings-page {
    padding: 20px;
  }
  
  .setting-item {
    flex-direction: column;
    align-items: flex-start;
    gap: 16px;
  }
  
  .setting-control {
    width: 100%;
  }
  
  .el-radio-group {
    width: 100%;
    flex-direction: column;
  }
  
  .el-radio-button {
    width: 100%;
  }
  
  .el-radio-button__inner {
    width: 100%;
    text-align: left;
  }
}
</style>